import { Difficulty, GameMode, world } from "@minecraft/server";
import { PlayerHealthIndicator } from "@sapi-game/gameComponent";
import { RespawnComponent } from "@sapi-game/gameComponent/player/respawn";
import { PlayerRegionMonitor } from "@sapi-game/gameComponent/player/regionMonitor";
import { PlayerGroup } from "@sapi-game/gamePlayer";
import { Duration } from "@sapi-game/utils";
import { BlockedInCombatDaemon } from "../components/daemon";
import { BlockedInCombatSpawnPoint } from "../components/playerSpawn";
import { BlockedPortalCleaner } from "../components/portalCleaner";
import { BlockedTeamColorMap, BlockedTeamData } from "../interface";
import { BlockedInCombatModule } from "../module";
import { BlockedInCombatPlayer } from "../player";
import { BlockedEndState } from "./endState";

export class BlockedMainState extends BlockedInCombatModule.State {
    override onEnter(): void {
        this.addComponent(BlockedInCombatSpawnPoint)
            .addComponent(
                RespawnComponent<BlockedInCombatPlayer, BlockedTeamData>,
                {
                    groupSet: this.context.groupSet,
                    onDie(player) {
                        if (player.live > 0) {
                            player.live--;
                        }
                        if (player.live <= 0) {
                            player.player?.setGameMode(GameMode.Spectator);
                        }
                    },
                    autoBroadcast: true,
                    buildNameFunc(player, group) {
                        return `${BlockedTeamColorMap[group.data.color].color}${
                            player.name
                        }`;
                    },
                    buildMsg(playerName, killerName, player) {
                        return killerName
                            ? `${playerName} §r 被 ${killerName} §r 杀死了 (剩余 ${player.live} 命)`
                            : `${playerName} §r 死了 (剩余 ${player.live} 命)`;
                    },
                }
            )
            .addComponent(PlayerHealthIndicator, {
                scoreBoardName: "healthIndicator",
                displayName: "§c血量",
                refreshInterval: new Duration(10),
            })
            .addComponent(BlockedInCombatDaemon)
            .addComponent(BlockedPortalCleaner)
            //限制玩家区域
            .addComponent(PlayerRegionMonitor<BlockedInCombatPlayer>, {
                region: this.context.map.region,
                onLeave(player) {
                    if (player.live <= 0) return;
                    player.player?.kill();
                },
                interval: new Duration(10),
                groups: this.context.groupSet,
            });
        this.init();
    }

    init() {
        const spawn = this.getComponent(BlockedInCombatSpawnPoint);
        const healthIndicator = this.getComponent(PlayerHealthIndicator);
        spawn.resetAllSpawnAreas();
        spawn.tpAllPlayersToSpawn();
        this.context.groupSet.forEach((p) => {
            p.live = this.context.config.liveCnt; //初始化生命数
            p.player.setGameMode(GameMode.Survival);
            p.title("开战");
            p.player.resetLevel();
            p.player.addLevels(60);
        });
        healthIndicator.show();
        //设置游戏规则
        world.setDifficulty(Difficulty.Normal);
        world.gameRules.fallDamage = true;
        world.gameRules.pvp = true;
    }

    end(team?: PlayerGroup<BlockedInCombatPlayer, BlockedTeamData>) {
        if (this.context.winner) return;
        this.context.winner = team;
        this.transitionTo(BlockedEndState);
    }
}
